import {defineStore} from 'pinia';
import {Http, Cache, Helper, Constant, Msg} from 'core';
const store = {
    id: "store",
    state() {
        return {
            pageNo: 1,
            pages: 0,
            records: [],
            pageSize: 10,
            total: 0,
            condition:{},
            hasMore: true,

            staffInfo: {},
        }
    },

    getters: {},

    actions: {
        /**
         * 查询我的成员列表
         * @param keyword 手机号
         * @param orderState
         * @param startTime
         * @param endTime
         * @returns {Promise<void>}
         */
        async getList(pageNo, pageSize, condition) {
            if (pageNo == 1) {
                this.records = [];
                this.hasMore = true;
            }

            if(! this.hasMore) {
                return;
            }

            this.condition = condition;
            let params = {current: pageNo, size: pageSize};
            if(! Helper.emptyJson(this.condition)) {
                params = Object.assign(params, this.condition);
            }

            Msg.loading("加载中...");
            let {code, data} = await Http.get("/staff/page", params);
            Msg.loading(false);
            // Log.info("获取我的订单列表", code, data);
            if (code == 200) {
                this.pages = data.pages;
                this.pageNo = data.current;
                this.pageSize = data.size;
                this.total = data.total;
                if(Helper.emptyArray(data.records)) {
                    this.hasMore = false;
                }
                this.records = this.records.concat(data.records);
            }

            return this.records;
        },

        /**
         * 获取门店信息
         * @param id
         * @returns {Promise<[]|*>}
         */
        async getInfo(id) {
            Msg.loading("加载中...")
            this.storeInfo = [];
            let {code, data} = await Http.get("/staff/info/" + id)
            Msg.loading(false);
            if(code == 200){
                if(! Helper.emptyJson(data) && ! Helper.emptyArray(data.roleList)) {
                    data.roleCode = data.roleList[0].roleCode;
                }
                this.storeInfo = data;
            }
            return this.storeInfo;
        },

        /**
         * 成员保存
         * @param form
         */
        async save(form) {

            if (!this.validate(form)) {
                return;
            }

            if(Helper.empty(form.password)) {
                Msg.error("账号密码不能为空");
                return false;
            }

            Msg.loading("加载中...");
            let {code, data} = await Http.post("/staff/save", form);
            Msg.loading(false);

            if (code == 200) {
                if (data) {
                    Msg.success("保存成员成功");
                } else {
                    Msg.error("保存成员失败");
                }
                return data;
            }
            return false;
        },

        /**
         * 门店保存
         * @param form
         */
        async update(form) {
            if (!this.validate(form)) {
                return;
            }


            Msg.loading("加载中...");
            let {code, data} = await Http.post("/staff/update", form);
            Msg.loading(false);

            if (code == 200) {
                if (data) {
                    Msg.success("修改成员成功");
                } else {
                    Msg.error("修改成员失败");
                }
                return data;
            }
            return false;
        },

        /**
         * 验证
         * @param form
         */
        validate(form) {
            if(Helper.empty(form.name)) {
                Msg.error("姓名不能为空");
                return false;
            }

            if(Helper.empty(form.username)) {
                Msg.error("账号不能为空");
                return false;
            }

            if(Helper.empty(form.storeIds)) {
                Msg.error("请选择门店");
                return false;
            }
            return true;
        }
    }
}

export default defineStore(store)
